package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.ProductionProject;
import com.ruoyi.factorydata.domain.ProductionProjectProductionInfo;
import com.ruoyi.factorydata.domain.vo.ProductionProjectProductionInfoVo;
import com.ruoyi.factorydata.mapper.ProductionProjectMapper;
import com.ruoyi.factorydata.mapper.ProductionProjectProductionInfoMapper;
import com.ruoyi.factorydata.mapper.ProjectProductionPlanMapper;
import com.ruoyi.factorydata.mapper.WorkOrderInfoMapper;
import com.ruoyi.factorydata.service.IProductionProjectProductionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 生产项目产品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Slf4j
@Service
public class ProductionProjectProductionInfoServiceImpl extends ServiceImpl<ProductionProjectProductionInfoMapper, ProductionProjectProductionInfo> implements IProductionProjectProductionInfoService {


    private final ProductionProjectMapper productionProjectMapper;
    private final ProductionProjectProductionInfoMapper productionProjectProductionInfoMapper;
    private final WorkOrderInfoMapper workOrderInfoMapper;
    private final ProjectProductionPlanMapper projectProductionPlanMapper;

    public ProductionProjectProductionInfoServiceImpl(ProductionProjectMapper productionProjectMapper,
                                                      ProductionProjectProductionInfoMapper productionProjectProductionInfoMapper,
                                                      WorkOrderInfoMapper workOrderInfoMapper,
                                                      ProjectProductionPlanMapper projectProductionPlanMapper) {
        this.productionProjectMapper = productionProjectMapper;
        this.productionProjectProductionInfoMapper = productionProjectProductionInfoMapper;
        this.workOrderInfoMapper = workOrderInfoMapper;
        this.projectProductionPlanMapper = projectProductionPlanMapper;
    }

    /**
     * 查询生产项目产品
     *
     * @param id 生产项目产品主键
     * @return 生产项目产品
     */
    @Override
    public ProductionProjectProductionInfo selectProductionProjectProductionInfoById(Long id) {
        return productionProjectProductionInfoMapper.selectProductionProjectProductionInfoById(id);
    }

    /**
     * 查询生产项目产品列表
     *
     * @param productionProjectProductionInfo 生产项目产品
     * @return 生产项目产品
     */
    @Override
    public List<ProductionProjectProductionInfoVo> selectProductionProjectProductionInfoList(ProductionProjectProductionInfo productionProjectProductionInfo) {
        List<ProductionProjectProductionInfo> productionProjectProductionInfos = productionProjectProductionInfoMapper.selectProductionProjectProductionInfoList(productionProjectProductionInfo);
        HashMap<Long, ProductionProject> longProductionProjectHashMap = new HashMap<>();
        List<ProductionProjectProductionInfoVo> voList = productionProjectProductionInfos.stream().map(projectProductionInfo -> {
            ProductionProject productionProject = this.selectProductionProjectByIdAndCache(projectProductionInfo.getProjectId(), longProductionProjectHashMap);
            //获取项目实际产量
            Long actualProduction = workOrderInfoMapper.sumActualProductionByProjectId(projectProductionInfo.getProjectId());
            Long scheduledProduction = projectProductionPlanMapper.sumScheduledProductionByProject(projectProductionInfo.getProjectId(), projectProductionInfo.getMaterialId());
            return ProductionProjectProductionInfoVo.builder()
                    .id(projectProductionInfo.getId())
                    .projectId(projectProductionInfo.getProjectId())
                    .productionBomId(projectProductionInfo.getProductionBomId())
                    .materialId(projectProductionInfo.getMaterialId())
                    .materialCode(projectProductionInfo.getMaterialCode())
                    .materialName(projectProductionInfo.getMaterialName())
                    .materialSpecification(projectProductionInfo.getMaterialSpecification())
                    .materialUnit(projectProductionInfo.getMaterialUnit())
                    .materialUnitName(projectProductionInfo.getMaterialUnitName())
                    .plannedProduction(projectProductionInfo.getPlannedProduction())
                    .scheduledProduction(Optional.ofNullable(scheduledProduction).orElse(0L).toString())
                    .actualProduction(Optional.ofNullable(actualProduction).orElse(0L).toString())
                    .deliveryDate(projectProductionInfo.getDeliveryDate())
                    .status(projectProductionInfo.getStatus())
                    .projectStatus(productionProject.getStatus())
                    .delFlag(projectProductionInfo.getDelFlag())
                    .version(projectProductionInfo.getVersion())
                    .productionProject(productionProject)
                    .build();
        }).collect(Collectors.toList());
        Page<ProductionProjectProductionInfo> queryData=(Page<ProductionProjectProductionInfo>)productionProjectProductionInfos;
        Page<ProductionProjectProductionInfoVo> page = new Page<>(queryData.getPageNum(), queryData.getPageSize());
        page.setTotal(queryData.getTotal());
        page.addAll(voList);
        return page;
    }

    /**
     * 新增生产项目产品
     *
     * @param productionProjectProductionInfo 生产项目产品
     * @return 结果
     */
    @Override
    public int insertProductionProjectProductionInfo(ProductionProjectProductionInfo productionProjectProductionInfo) {
        productionProjectProductionInfo.setCreateTime(DateUtils.getNowDate());
        return productionProjectProductionInfoMapper.insertProductionProjectProductionInfo(productionProjectProductionInfo);
    }

    /**
     * 修改生产项目产品
     *
     * @param productionProjectProductionInfo 生产项目产品
     * @return 结果
     */
    @Override
    public int updateProductionProjectProductionInfo(ProductionProjectProductionInfo productionProjectProductionInfo) {
        productionProjectProductionInfo.setUpdateTime(DateUtils.getNowDate());
        return productionProjectProductionInfoMapper.updateProductionProjectProductionInfo(productionProjectProductionInfo);
    }

    /**
     * 批量删除生产项目产品
     *
     * @param ids 需要删除的生产项目产品主键
     * @return 结果
     */
    @Override
    public int deleteProductionProjectProductionInfoByIds(Long[] ids) {
        return productionProjectProductionInfoMapper.deleteProductionProjectProductionInfoByIds(ids);
    }

    /**
     * 删除生产项目产品信息
     *
     * @param id 生产项目产品主键
     * @return 结果
     */
    @Override
    public int deleteProductionProjectProductionInfoById(Long id) {
        return productionProjectProductionInfoMapper.deleteProductionProjectProductionInfoById(id);
    }

    @Override
    public ProductionProject selectProductionProjectByIdAndCache(Long id, Map<Long, ProductionProject> cache) {
        return CacheSupportUtils.getFromCacheOrDB(id, cache, productionProjectMapper::selectProductionProjectAndCustomerNameById, productionProject -> productionProject, false);
    }


}